<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Offices extends My_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('franchise_model', 'franchise');
    }

    public function index() {
        $this->navigation->loadFindOfficesView();
    }

    public function find() {
        $this->load->model('au_suburb_geo_model', 'suburb');
        $suburb_id = $this->uri->segment(3);
        if (!empty($suburb_id)) {            
            $suburd_Geo = $this->suburb->getSuburbById($suburb_id);
            $suburbGeo = $this->suburb->getSuburbGeoById($suburb_id);
            //var_dump($suburbGeo);die;
//            if (!empty($suburbGeo)){}
//                $data['office'] = $this->franchise->getOfficeNearest($suburbGeo->Lat, $suburbGeo->Long);
            if(!empty($suburd_Geo)){
                $data['office'] = $suburd_Geo;
                //var_dump($suburd_Geo);die;
            }
            else{
                if (!empty($suburbGeo)){
                    $data['office'] = $this->franchise->getOfficeNearest($suburbGeo->Lat, $suburbGeo->Long);
                    //var_dump($data['office']);die;
                }
            }
        }
        $data['franchiseQLD'] = $this->franchise->loadFranchise(array('location_addr_state' => 'QLD', 'status' => 1));
        $data['franchiseSA'] = $this->franchise->loadFranchise(array('location_addr_state' => 'SA', 'status' => 1));
        $data['franchiseNSW'] = $this->franchise->loadFranchise(array('location_addr_state' => 'NSW', 'status' => 1));

        $this->navigation->loadFindOfficesView($data);
    }

    public function office() {
        $this->load->model('franchise_suburbs_model', 'suburbs');
         $this->load->model('testimonials_model', 'testimonials');
        $id = $this->uri->segment(3);
        if (!$id)
            redirect('offices');
        $franchise = $this->franchise->getFrachiseById($id);
        if (!$franchise)
            redirect('offices');
        $data['franchise'] = $franchise;
        $data['suburbs'] = $this->suburbs->loadFranchiseSuburbs(array('franchise_suburbs.franchise_id' => $id, 'franchise_suburbs.status' => 1));
        $data['lastestTes']=$this->testimonials->getLastestTestimonial($id);
        
        $this->navigation->loadOfficeView($data);
    }

    public function office_email() {
        $posts = $this->input->post();
        $id = $this->uri->segment(3);
        $franchise = $this->franchise->getFrachiseById($id);
        $emailTo = !empty($franchise->email) ? $franchise->email : null;
        $validationErrors = array();
        $this->load->helper('email');
        if ($posts['emailoffice_name'] == '') {
            $validationErrors['emailoffice_name'] = "Your name cannot be blank";
        }
        if ($posts['emailoffice_email'] == '') {
            $validationErrors['emailoffice_email'] = "Email cannot be blank";
        } elseif (!valid_email($posts['emailoffice_email'])) {
            $validationErrors['emailoffice_email'] = "Email is not valid";
        }



        if (count($validationErrors) != 0) {
            $data['usermessage'] = array('error', 'Please complete all required fields', '0');
            $data['fielderrors'] = $validationErrors;
        } else {
            $content = '<b>Office Name</b> :' . $posts['emailoffice_name'] . '<br>';
            $content .= '<b>Email</b>  :' . $posts['emailoffice_email'] . '<br>';
            $content .= '<b>Mobile</b>  :' . $posts['emailoffice_mobile'] . '<br>';
            $content .= '<b>Message</b>  :' . $posts['emailoffice_message'] . '<br>';
            if (sendmail($emailTo, 'Office emaill request', $content, $posts['emailoffice_email'], $posts['emailoffice_name'])) {
                $data['usermessage'] = array('success', 'Thank you - Your email request is being sent and a Sales Agent will be in contact with you directly');
                $data['post'] = '';
            } else {
                $data['usermessage'] = array('error', 'Email sent Error', '1');
            }
        }
        echo json_encode($data);
    }

    public function marketappraisal_email() {
        $posts = $this->input->post();
        $id = $this->uri->segment(3);
        $franchise = $this->franchise->getFrachiseById($id);
        $emailTo = !empty($franchise->email) ? $franchise->email : null;
        $validationErrors = array();
        $this->load->helper('email');
        if ($posts['marketappraisal_name'] == '') {
            $validationErrors['marketappraisal_name'] = "Your name cannot be blank";
        }
        if ($posts['marketappraisal_email'] == '') {
            $validationErrors['marketappraisal_email'] = "Email cannot be blank";
        } elseif (!valid_email($posts['marketappraisal_email'])) {
            $validationErrors['marketappraisal_email'] = "Email is not valid";
        }

        if (count($validationErrors) != 0) {
            $data['usermessage'] = array('error', 'Please complete all required fields', '0');
            $data['fielderrors'] = $validationErrors;
        } else {
            $content = '<b>Marketappraisal Name</b> :' . $posts['marketappraisal_name'] . '<br>';
            $content .= '<b>Marketappraisal Email</b>  :' . $posts['marketappraisal_email'] . '<br>';
            $content .= '<b>Marketappraisal Mobile</b>  :' . $posts['marketappraisal_mobile'] . '<br>';
            $content .= '<b>Marketappraisal Address</b>  :' . $posts['marketappraisal_address'] . '<br>';
            $content .= '<b>Marketappraisal Address</b>  :' . $posts['marketappraisal_address'] . '<br>';
            $content .= '<b>Marketappraisal type</b>  :' . $posts['marketappraisal_type'] . '<br>';
            $content .= '<b>Marketappraisal propertytype</b>  :' . $posts['marketappraisal_propertytype'] . '<br>';
            $content .= '<b>Marketappraisal propertyage</b>  :' . $posts['marketappraisal_propertyage'] . '<br>';
            $content .= '<b>Marketappraisal bedrooms</b>  :' . $posts['marketappraisal_bedrooms'] . '<br>';
            $content .= '<b>Marketappraisal bathrooms</b>  :' . $posts['marketappraisal_bathrooms'] . '<br>';
            $content .= '<b>Marketappraisal landarea</b>  :' . $posts['marketappraisal_landarea'] . '<br>';
            $content .= '<b>Marketappraisal additionalinfo</b>  :' . $posts['marketappraisal_additionalinfo'] . '<br>';
            $content .= '<b>Marketappraisal comments</b>  :' . $posts['marketappraisal_comments'] . '<br>';
            if (sendmail($emailTo, 'Market appraisal request', $content, $posts['marketappraisal_email'], $posts['marketappraisal_name'])) {
                $data['usermessage'] = array('success', 'Thank you - Your email request is being sent and a Sales Agent will be in contact with you directly', '');
                $data['post'] = '';
            } else {
                $data['usermessage'] = array('error', 'Email sent Error', '1');
            }
        }
        echo json_encode($data);
    }

    public function team() {
        $this->load->model('franchise_tearm_model', 'tearm');
        $id = $this->uri->segment(3);
        if (!$id)
            redirect('offices');
        $data['tearms'] = $this->tearm->loadTearmsByFrachiseId($id);
        $data['franchise'] = $this->franchise->getFrachiseById($id);
        $this->navigation->loadOfficeTeamView($data);
    }

    public function emailagent_email() {
        $this->load->model('franchise_tearm_model', 'tearm');
        $posts = $this->input->post();
        $id = $posts['emailagent_id'];
        $tearm = $this->tearm->getFrachiseTearmById($id);
        $emailTo = $tearm->email;
        $validationErrors = array();
        $this->load->helper('email');
        if ($posts['emailagent_name'] == '') {
            $validationErrors['emailagent_name'] = "Your name cannot be blank";
        }
        if ($posts['emailagent_email'] == '') {
            $validationErrors['emailagent_email'] = "Email cannot be blank";
        } elseif (!valid_email($posts['emailagent_email'])) {
            $validationErrors['emailagent_email'] = "Email is not valid";
        }

        if (count($validationErrors) != 0) {
            $data['usermessage'] = array('error', 'Please complete all required fields', '0');
            $data['fielderrors'] = $validationErrors;
        } else {
            $content = '<b>Name</b> :' . $posts['emailagent_name'] . '<br>';
            $content .= '<b>Email</b>  :' . $posts['emailagent_email'] . '<br>';
            $content .= '<b>Mobile</b>  :' . $posts['emailagent_mobile'] . '<br>';
            $content .= '<b>Message</b>  :' . $posts['emailagent_message'] . '<br>';
            if (sendmail($emailTo, 'Meet the team email request', $content, $posts['emailagent_email'], $posts['emailagent_name'])) {
                $data['usermessage'] = array('success', 'Thank you - Your email request is being sent and a Sales Agent will be in contact with you directly');
                $data['post'] = '';
            } else {
                $data['usermessage'] = array('error', 'Email sent Error', '1');
            }
        }
        echo json_encode($data);
    }

    public function testimonial() {
        $this->load->model('Testimonials_model', 'testimonials');
        $franchise_id = (int) $this->uri->segment(3);
        if (empty($franchise_id))
            redirect($_SERVER['HTTP_REFERER']);
        $data['testimonialslist'] = $this->testimonials->loadTestimonialsListById($franchise_id);
        $data['franchisename'] = $this->testimonials->loadFranchiseNameById($franchise_id);

        $this->navigation->loadOfficeTestimonialView($data);
    }

    public function get_office() {
        $this->load->model('listing_model', 'listing');
        $style = $this->uri->segment(3);
        if(!$style)
            $style = 'listings';
        $skip = $this->uri->segment(4);
        if (!$skip)
            $skip = 0;
        
        $officeId = $this->uri->segment(5);
        $data['listting'] = array();
        $data['images'] = array();
        $listing = $this->listing->getCurrent($style, array('franchise_id' => $officeId),array(), 1, $skip, 'created_date', 'DESC');
        if (!empty($listing)){
            $data['listting'] = $listing[0];
            $data['style'] = $style;
            $data['images'] = base_url() . 'img/noimage.png';
            $mainImage = $this->listing->getImage($listing[0]->id);
            if ($mainImage != false) {
                $data['images'] = $mainImage->image_url;
            }
            echo $this->load->view('offices/get_office',$data);
        }
        
    }

}
